package org.example.task;

import org.springframework.stereotype.Component;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;

@Component
public class TimePrintTask implements Runnable {

    private final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private int countdown = 60; // 倒计时60秒

    @Override
    public void run() {
        while (countdown > 0) {
            Date now = new Date();
            System.out.println("当前时间: " + sdf.format(now) + " | 倒计时剩余: " + countdown + "秒");
            countdown--;
            try {
                TimeUnit.SECONDS.sleep(1); // 每秒执行一次
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
                break;
            }
        }
        System.out.println("倒计时结束!");
    }
}